import { GetKnotRecommendTopicsRequest, GetKnotRecommendTopicsResponse, RegenKnotRequest} from "@/types/proto/api/v1/knot_service";
import { ListKnotExchangesRequest, ListKnotExchangesResponse, GetKnotRequest} from "@/types/proto/api/v1/knot_service";
import { KnotServiceDefinition, CreateKnotRequest, CreateKnotChatRequest,Knot, DeleteKnotRequest} from "@/types/proto/api/v1/knot_service";
import { ListKnotsRequest, ListKnotsResponse, ListKnotChatsRequest, ListKnotChatsResponse } from "@/types/proto/api/v1/knot_service";
import { UpsertKnotReactionRequest, DeleteKnotReactionRequest} from "@/types/proto/api/v1/knot_service";
import { Exchange, ListExchangesRequest, ListExchangesResponse} from "@/types/proto/api/v1/exchange_service";

import { Reaction} from "@/types/proto/api/v1/reaction_service";

import { exchangeServiceClient, knotServiceClient} from "./grpcweb_auth";

import { Empty } from "@/types/proto/google/protobuf/empty";
import {useUserStore} from "@/store/user"

export async function CreateKnot(data: CreateKnotRequest): Promise<Knot> {
  try {
    const store = useUserStore();
    return knotServiceClient.createKnot(data)
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function GeteKnot(data: GetKnotRequest): Promise<Knot> {
  try {
    const store = useUserStore();
    return knotServiceClient.getKnot(data)
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function CreateKnotChat(data: CreateKnotChatRequest): Promise<Knot> {  
  try {
    const store = useUserStore();
    return knotServiceClient.createKnotChat(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListKnots(data: ListKnotsRequest): Promise<ListKnotsResponse> {
  try {
    const store = useUserStore();
    return knotServiceClient.listKnots(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListChats(data: ListKnotChatsRequest): Promise<ListKnotChatsResponse> {
  try {
    const store = useUserStore();
    return knotServiceClient.listKnotChats(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function UpsertKnotReaction(data: UpsertKnotReactionRequest): Promise<Reaction> {
  try {
    const store = useUserStore();
    return knotServiceClient.upsertKnotReaction(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function DeleteKnotReaction(data: DeleteKnotReactionRequest): Promise<Empty> {
  try {
    const store = useUserStore();
    return knotServiceClient.deleteKnotReaction(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function DeleteKnot(data: DeleteKnotRequest): Promise<Empty> {
  try {
    const store = useUserStore();
    return knotServiceClient.deleteKnot(data)
  } 
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function ListExchangeRecord(data: ListKnotExchangesRequest): Promise<ListKnotExchangesResponse> {
  try {
    return knotServiceClient.listKnotExchanges(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export function DecodeExchange(data:any): Exchange {
  try {
    let record = Exchange.decode(data);
    return record;  
  } catch(error) {
    console.error('DecodeExchange failed:', error);
    throw error;
  }
}

export async function GetRecommendTopic(data: GetKnotRecommendTopicsRequest): Promise<GetKnotRecommendTopicsResponse> {
  try {
    return knotServiceClient.getKnotRecommendTopic(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function RegenKnotChat(data: RegenKnotRequest): Promise<Knot> {
  try {
    return knotServiceClient.regenKnotChat(data)
  }
  catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}